package com.m3839.sdk.common.base;

import android.net.http.SslError;
import android.view.View;
import android.webkit.*;

import com.m3839.sdk.common.util.LogUtils;
import com.m3839.sdk.common.util.WebUtils;

/**
 * @description: 公共通用的WebDialogFragment
 *
 * @author ChenBaoyang
 * @date 7/22/21 17:31
 */
public abstract class BaseWebDialogFragment extends BaseDialogFragment {

    protected WebView webView;

    @Override
    protected String getLayoutName() {
        return "common_web";
    }

    @Override
    protected void parseArgs() {

    }

    @Override
    protected void initView(View view) {
        webView = view.findViewById(getId("webView"));
    }

    @Override
    protected void setSystemUi() {
        int uiOptions =
                //全屏
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                //隐藏导航栏
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        uiOptions = uiOptions | 0x00001000;
        getDialog().getWindow().getDecorView().setSystemUiVisibility(uiOptions);
    }

    @Override
    protected void setContent() {
        WebUtils.setWebSettings(webView);
        webView.setBackgroundColor(0);
        webView.getBackground().setAlpha(0);
        webView.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });



        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LogUtils.flog("加载完成");
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                LogUtils.flog("onReceivedError：" + error.toString());
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });

        setWebContent();
    }

    protected abstract void setWebContent();

    @Override
    protected void initListener() {

    }

}